package com.joseph.websocket.client;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.gson.Gson;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import javassist.CtClass;
import org.apache.commons.lang3.time.StopWatch;

import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;

public class HeartbeatHandler extends IdleStateHandler {

    public HeartbeatHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) {
        super(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent)evt;
            if (event == IdleStateEvent.ALL_IDLE_STATE_EVENT) {
                ctx.channel().writeAndFlush("node heartbeat");
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }

    public static void main(String[] args) {
        StopWatch started = StopWatch.createStarted();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(started.getTime());
        AtomicInteger a = new AtomicInteger(Integer.MAX_VALUE);
    }
}
